﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace Server
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            EventDrivenShare PushToGUI = new EventDrivenShare();

            Network NetStream = new Network(PushToGUI);
            Thread NetworkThread = new Thread(NetStream.Run);
            NetworkThread.IsBackground = true; //Threaden lukkes ned når main-thread lukkes (GUI) (Når der trykkes på kryds/alt+f4 så lukker den tråden)

            Form1 GUI = new Form1();

            GuiInputHandler GuiEventListener = new GuiInputHandler(GUI);
            PushToGUI.ChatMessageChange += GuiEventListener.PushChatMessage;


            NetworkThread.Start();
            Application.Run(GUI);
        }

        /// <summary>
        /// Klasse der håndtere events kald til GUIen
        /// </summary>
        class GuiInputHandler
        {
            private Form1 GUI;

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="GuiIn">Form1 events skal sende videre til</param>
            public GuiInputHandler(Form1 GuiIn)
            {
                GUI = GuiIn;
            }

            /// <summary>
            /// Håndtere eventet ChatmessageChange
            /// </summary>
            /// <param name="sender">Afsender af eventet</param>
            /// <param name="e">data der blev sendt med eventet</param>
            public void PushChatMessage(object sender, EventDrivenShare.ChatMessageArgs e)
            {
                GUI.BeginInvoke(GUI.DelegateChatMessage, e); //Sætter kaldet i kø hos guien
            }




        }
    }
}
